<?php

namespace mybackend\controllers\picture;

/**
 * controller for 租户管理->图片管理
 */
use Yii;
use mybackend\controllers\BaseController;
use common\error\ErrorCode;
use common\entities\myhtsq\ActionCode;
use common\services\picture\PictureService;

class PictureController extends BaseController {
    public $_pictureService;
    public function __construct($id, $module,  PictureService $pictureService, $config = []) {
        $this->_pictureService = $pictureService;
        parent::__construct($id, $module, $config);
    }

    protected function roleAuthRules() {
        return [
            'actions' => ['Save', 'Edit', 'Delete', 'WhetherAble', 'List'],
            'rules' => [
                [
                    'allow' => true,
                    'actionCodeMaps' => [
                        'Save' => ActionCode::EXP_PICTURE_MOD,
                        'Edit' => ActionCode::EXP_PICTURE_MOD,
                        'Delete' => ActionCode::EXP_PICTURE_MOD,
                        'WhetherAble' => ActionCode::EXP_PICTURE_MOD,
                        'List' => ActionCode::EXP_PICTURE_VIEW,
                    ]
                ]
            ]
        ];
    }

    /**
     * 添加或修改图片信息
     */
    public function actionSave()
    {
        $pictureData = Yii::$app->request->post();
        if(empty($pictureData["type"]) || empty($pictureData['title']) || empty($pictureData["sort"]) || empty($pictureData["picture"])){
            return $this->json([
                "retCode"=> ErrorCode::ERR_PICTURE_INVALID_FALL,
                "msg" => "必填项未填",
                "data"=>['result'=> false]
            ]);
        }

        if ($pictureData["type"] == 1 && empty($pictureData["bg_color"])){
            return $this->json([
                "retCode"=> ErrorCode::ERR_PICTURE_INVALID_FALL,
                "msg" => "登录页图片底纹颜色必填",
                "data"=>['result'=> false]
            ]);
        }

        $saveResult = $this->_pictureService->save($pictureData);

        return $this->json([
            "retCode"=> $this->_pictureService->getErrCode(),
            "msg" => $this->_pictureService->getErrMsg(),
            "data"=>['result'=> $saveResult]
        ]);
    }

    /**
     * 编辑页图片信息
     */
    public function actionEdit($id)
    {
        $picInfo = $this->_pictureService->getPicInfoById($id);
        return $this->json([
            "retCode"=> $this->_pictureService->getErrCode(),
            "msg" => $this->_pictureService->getErrMsg(),
            "data"=>$picInfo
        ]);

    }

    /**
     * 编辑页图片信息
     */
    public function actionDelete($id)
    {
        $deleteResult = $this->_pictureService->delete($id);
        return $this->json([
            "retCode"=> $this->_pictureService->getErrCode(),
            "msg" => $this->_pictureService->getErrMsg(),
            "data" => ["result"=>$deleteResult]
        ]);
    }

    /**
     * 是否启用图片
     */
    public function actionWhetherAble($id,$is_able)
    {
        $saveResult = $this->_pictureService->updateAble($id,$is_able);
        return $this->json([
            "retCode"=> $this->_pictureService->getErrCode(),
            "msg" => $this->_pictureService->getErrMsg(),
            "data" => ["result"=>$saveResult]
        ]);
    }

    /**
     * 后台管理图片列表
     */
    public function actionList()
    {
        $keyword = Yii::$app->request->get("keyword","");
        $page = Yii::$app->request->get("page", 0);
        $perPage = Yii::$app->request->get("per_page", 10);
        $picList = $this->_pictureService->getPicList($keyword, $page, $perPage);
        return $this->json([
            "retCode"=> $this->_pictureService->getErrCode(),
            "msg" => $this->_pictureService->getErrMsg(),
            "data" => $picList[0],
            "total" => $picList[1]
        ]);
    }
}
